﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using LevelEditor.model.elements;
using System.Windows.Forms;

namespace LevelEditor.model.core
{
    public class Library
    {
        #region Variables
        public const String BARREL_TYPE = "barrel";
        public const String CRATE_TYPE = "crate";
        public const String CHARACTER_TYPE = "character";
        public const String TYPE_KEY = "type";
        public const String SIZE_X_KEY = "sizeX";
        public const String SIZE_Y_KEY = "sizeY";
        private const String XML_LIB_URL = "../../xml/lib";
        private const String NAME_KEY = "name";
        private const String URL_KEY = "url";
        private const String XML_FILENAME_KEY = "filename";

        private static List<Dictionary<String, String>> m_pGameObjectsDefinitions = new List<Dictionary<String, String>>();
        private static List<String> m_libNamesList = new List<String>();
        private static List<Bitmap> m_libPicturesList = new List<Bitmap>();
        private static List<String> m_usedAssetsList = new List<String>();
        private static int m_pGameObjectsDefinitionsNb = 0;

        #endregion

        #region Constructeurs prives - pas d'instanciation
        private Library() { }

        #endregion

        #region Methodes
        //TODO: Parcours du repertoire xml
        public static void load()
        {
            String[] files = null;

            try
            {
                files = System.IO.Directory.GetFiles(XML_LIB_URL);
            }
            catch (System.Exception ex)
            {
            	Console.WriteLine("Impossible de charger la librairie : " + ex.Message);
                return;
            }
            

            for (int i = 0; i < files.Length; ++i)
            {
                addLibrary(ParseXML.readXMLLibrary(files[i]));
            }
        }

        public static void addLibrary(Dictionary<String, String> pGameObjectDefinition)
        {
            if (m_pGameObjectsDefinitions.Contains(pGameObjectDefinition))
            {
                Console.WriteLine("Definitions d'objets deja presente dans la librairie");
                return;
            }

            m_pGameObjectsDefinitions.Add(pGameObjectDefinition);
            m_pGameObjectsDefinitionsNb++;
        }

        public static void init()
        {
            m_libNamesList = new List<String>();
            m_libPicturesList = new List<Bitmap>();
            PictureBox smallImg = null;
            Bitmap thumbnail = null;

            for (int i = 0; i < m_pGameObjectsDefinitionsNb; ++i)
            {
                m_libNamesList.Add(m_pGameObjectsDefinitions[i][NAME_KEY]);

                try
                {
                    smallImg = new PictureBox();
                    smallImg.Image = Image.FromFile(m_pGameObjectsDefinitions[i][URL_KEY]);
                    thumbnail = new Bitmap(Convert.ToInt32(m_pGameObjectsDefinitions[i][SIZE_X_KEY]), Convert.ToInt32(m_pGameObjectsDefinitions[i][SIZE_Y_KEY]));
                    smallImg.DrawToBitmap(thumbnail, new Rectangle(0, 0, Convert.ToInt32(m_pGameObjectsDefinitions[i][SIZE_X_KEY]), Convert.ToInt32(m_pGameObjectsDefinitions[i][SIZE_Y_KEY])));
                    m_libPicturesList.Add(thumbnail);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message + m_pGameObjectsDefinitions[i][URL_KEY]);
                }
            }
        }

        #endregion

        #region Methodes pour les assets par level
        public static void addUsedAsset(String xmlFileName)
        {
            if (m_usedAssetsList.Contains(xmlFileName))
            {
                return;
            }

            m_usedAssetsList.Add(xmlFileName);
        }

        public static void clearUsedAssetsList()
        {
            m_usedAssetsList.Clear();
        }

        public static String getUrlLinkedToXml(String xmlFileName)
        {
            for (int i = 0; i < m_pGameObjectsDefinitionsNb; ++i)
            {
                Dictionary<String, String> pGameObjectDefinition = m_pGameObjectsDefinitions[i];
                
                if (pGameObjectDefinition[XML_FILENAME_KEY].Equals(xmlFileName))
                {
                    return pGameObjectDefinition[URL_KEY];
                }
            }

            return null;
        }

        public static String getXmlLinkedToUrl(String pictureUrl)
        {
            for (int i = 0; i < m_pGameObjectsDefinitionsNb; ++i)
            {
                Dictionary<String, String> pGameObjectDefinition = m_pGameObjectsDefinitions[i];

                if (pGameObjectDefinition[URL_KEY].Equals(pictureUrl))
                {
                    return pGameObjectDefinition[XML_FILENAME_KEY];
                }
            }

            return null;
        }

        public static int getResIdFromXmlFileName(String xmlFileName)
        {
            for (int i = 0; i < m_usedAssetsList.Count; ++i)
            {
                if(xmlFileName.Equals(m_usedAssetsList[i]))
                {
                    return i;
                }
            }

            return -1;
        }

        public static float getSizeXWithXmlFileName(String xmlFileName)
        {
            for (int i = 0; i < m_pGameObjectsDefinitionsNb; ++i)
            {
                Dictionary<String, String> pGameObjectDefinition = m_pGameObjectsDefinitions[i];

                if (pGameObjectDefinition[XML_FILENAME_KEY].Equals(xmlFileName))
                {
                    return float.Parse(pGameObjectDefinition[SIZE_X_KEY]);
                }
            }

            return -1.0f;
        }

        public static float getSizeYWithXmlFileName(String xmlFileName)
        {
            for (int i = 0; i < m_pGameObjectsDefinitionsNb; ++i)
            {
                Dictionary<String, String> pGameObjectDefinition = m_pGameObjectsDefinitions[i];

                if (pGameObjectDefinition[XML_FILENAME_KEY].Equals(xmlFileName))
                {
                    return float.Parse(pGameObjectDefinition[SIZE_Y_KEY]);
                }
            }

            return -1.0f;
        }

        #endregion

        #region Getters && Setters
        public static Bitmap getBmp(int id)
        {
            return m_libPicturesList[id];
        }

        public static Dictionary<String, String> getPGameObjectDefinition(int key)
        {
            return m_pGameObjectsDefinitions[key];
        }

        public static List<String> libNamesList
        {
            get { return m_libNamesList; }
        }

        public static List<String> usedAssetsList
        {
            get { return m_usedAssetsList; }
        }

        #endregion
    }
}
